/**
 * @file AccountType.c
 * @author 严一涵 (yyhhenry@foxmail.com)
 * @brief 账号管理
 * @version 0.1
 * @date 2022-05-11
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "./AccountType.h"

const char *const AccountSheetTitle = "借阅卡号,借阅人姓名,借阅数量限制,已经借阅的数量,借阅密码";

AccountType *AccountType_AccountType(const char *str)
{
    ListType *list = Str_split(str, ",", 0);
    ListNode *ite = list->head;
    AccountType *account = malloc(sizeof(AccountType));
    account->id = Str_clone(Str_ListNode(ite)), ite = ite->next;
    account->name = Str_clone(Str_ListNode(ite)), ite = ite->next;
    account->limit = Str_parseInt(Str_ListNode(ite)), ite = ite->next;
    account->count = Str_parseInt(Str_ListNode(ite)), ite = ite->next;
    account->password = Str_clone(Str_ListNode(ite)), ite = ite->next;
    ListType_free(list);
    return account;
}

char *AccountType_toCSVStr(AccountType *account)
{
    ListType *list = ListType_ListType(free);
    ListType_insert(list, NULL, Str_CSVMode(account->id));
    ListType_insert(list, NULL, Str_CSVMode(account->name));
    ListType_insert(list, NULL, Str_CSVModeFree(Str_fromInt(account->limit)));
    ListType_insert(list, NULL, Str_CSVModeFree(Str_fromInt(account->count)));
    ListType_insert(list, NULL, Str_CSVMode(account->password));
    char *res = Str_join(list, ",");
    ListType_free(list);
    return res;
}

AccountType *AccountType_ListNode(ListNode *p)
{
    return p->ptr;
}

void AccountType_free(void *_account)
{
    AccountType *account = _account;
    free(account->id);
    free(account->name);
    free(account->password);
    free(account);
}

AccountListType *AccountListType_AccountListType(const char *_filename)
{
    AccountListType *accountList = malloc(sizeof(AccountListType));
    accountList->filename = Str_clone(_filename);
    accountList->list = ListType_ListType(AccountType_free);
    AccountListType_read(accountList);
    return accountList;
}

void AccountListType_free(AccountListType *accountList)
{
    free(accountList->filename);
    ListType_free(accountList->list);
    free(accountList);
}

void AccountListType_read(AccountListType *accountList)
{
    char *fileContent = Str_fromFile(accountList->filename);
    ListType *tmpList = Str_split(fileContent, "\n", 1);
    while (accountList->list->size)
        ListType_remove(accountList->list, accountList->list->head);
    ListType_remove(tmpList, tmpList->head); // 删去表头
    while (tmpList->size)
    {
        char *tmpStr = Str_ListNode(tmpList->head); // 借用所有权，不销毁
        if (!Str_isSpace(tmpStr))
            ListType_insert(accountList->list, NULL, AccountType_AccountType(tmpStr));
        ListType_remove(tmpList, tmpList->head);
    }
    free(fileContent);
    ListType_free(tmpList);
}

void AccountListType_write(AccountListType *accountList)
{
    ListType *tmpList = ListType_ListType(free);
    ListType_insert(tmpList, NULL, Str_clone(AccountSheetTitle));
    for (ListNode *ite = accountList->list->head; ite; ite = ite->next)
    {
        ListType_insert(tmpList, NULL, AccountType_toCSVStr(AccountType_ListNode(ite)));
    }
    char *fileContent = Str_join(tmpList, "\n");
    Str_toFile(fileContent, accountList->filename);
    free(fileContent);
    ListType_free(tmpList);
}

ListNode *AccountListType_query(AccountListType *accountList, const char *id)
{
    for (ListNode *ite = accountList->list->head; ite; ite = ite->next)
    {
        AccountType *account = AccountType_ListNode(ite);
        if (strcmp(account->id, id) == 0 || strcmp(account->name, id) == 0)
            return ite;
    }
    return NULL;
}
